package com.koi.system.controller.notice;

import cn.dev33.satoken.annotation.SaCheckPermission;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.koi.common.anno.KoiLog;
import com.koi.common.enums.log.OperType;
import com.koi.common.ex.koi.YUtil;
import com.koi.common.satoken.KoiLoginUtil;
import com.koi.model.vo.system.SysNoticeVo;
import com.koi.pojo.system.SysNotice;
import com.koi.system.service.notice.SysNoticeService;
import jakarta.validation.constraints.NotNull;
import lombok.RequiredArgsConstructor;
import org.apache.commons.lang3.StringUtils;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.List;

/**
 * @author ･ᴗ･
 * @description [通知公告表-控制层]
 */
@Validated
@RequestMapping("/koi")
@RequiredArgsConstructor
@RestController
public class SysNoticeController {

    private final SysNoticeService sysNoticeService;

    /**
     * @description [分页查询]
     * @author ･ᴗ･
     */
    @SaCheckPermission("system:notice:list")
    @GetMapping("/sysNotice/listPage")
    public IPage<SysNotice> list(SysNoticeVo vo) {
        // 分页构造器
        Page<SysNotice> page = new Page<>(vo.getPageNo(), vo.getPageSize());
        // 条件构造器
        LambdaQueryWrapper<SysNotice> wrapper = new LambdaQueryWrapper<>();
        wrapper.like(StringUtils.isNotBlank(vo.getNoticeTitle()), SysNotice::getNoticeTitle, vo.getNoticeTitle());
        wrapper.eq(StringUtils.isNotBlank(vo.getNoticeStatus()), SysNotice::getNoticeStatus, vo.getNoticeStatus());
        wrapper.eq(StringUtils.isNotBlank(vo.getNoticeType()), SysNotice::getNoticeType, vo.getNoticeType());
        wrapper.orderByDesc(SysNotice::getSorted);
        // 进行分页查询
        page = sysNoticeService.page(page, wrapper);
        return page;
    }

    /**
     * @description [查询所有]
     * @author ･ᴗ･
     */
    @GetMapping("/sysNotice/list")
    public List<SysNotice> list() {
        return sysNoticeService.list();
    }

    /**
     * @description [查询一个]
     * @author ･ᴗ･
     */
    @GetMapping("/sysNotice/getById/{id}")
    public SysNotice getById(@PathVariable Long id) {
        return sysNoticeService.getById(id);
    }

    /**
     * @description [新增]
     * @author ･ᴗ･
     */
    @KoiLog(title = "新增公告", operType = OperType.ADD)
    @SaCheckPermission("system:notice:add")
    @PostMapping("/sysNotice/add")
    public void add(@Validated @RequestBody SysNotice sysNotice) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysNotice.setCreateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysNoticeService.save(sysNotice), "新增失败，请稍后重试");
    }

    /**
     * @description [修改]
     * @author ･ᴗ･
     */
    @KoiLog(title = "修改公告", operType = OperType.UPDATE)
    @SaCheckPermission("system:notice:update")
    @PostMapping("/sysNotice/update")
    public void update(@Validated @RequestBody SysNotice sysNotice) {
        if (StringUtils.isNotBlank(KoiLoginUtil.getUserName())) {
            sysNotice.setUpdateBy(KoiLoginUtil.getUserName());
        }
        YUtil.isTrue(!sysNoticeService.updateById(sysNotice), "修改失败，请稍后重试");
    }

    /**
     * @description [删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "删除公告", operType = OperType.DELETE)
    @SaCheckPermission("system:notice:delete")
    @PostMapping("/sysNotice/deleteById/{id}")
    public void delete(@PathVariable("id") Long id) {
        YUtil.isTrue(!sysNoticeService.removeById(id), "删除失败，请稍后重试");
    }

    /**
     * @description [批量删除]
     * @author ･ᴗ･
     */
    @KoiLog(title = "批量删除公告", operType = OperType.DELETE)
    @SaCheckPermission("system:notice:delete")
    @PostMapping("/sysNotice/batchDelete")
    public void batchDelete(@NotNull(message = "请选择需要删除的数据") @RequestBody List<Long> ids) {
        YUtil.isTrue(!sysNoticeService.removeBatchByIds(ids), "删除失败，请稍后重试");
    }

    /**
     * @description [修改状态]
     * @author ･ᴗ･
     */
    @PostMapping("/sysNotice/updateStatus/{noticeId}/{noticeStatus}")
    public void updateStatus(@PathVariable("noticeId") Long noticeId, @PathVariable("noticeStatus") String noticeStatus) {
        UpdateWrapper<SysNotice> updateWrapper = new UpdateWrapper<>();
        updateWrapper.set("notice_status", noticeStatus).eq("notice_id", noticeId);
        YUtil.isTrue(!sysNoticeService.update(updateWrapper), "修改失败，请稍后重试");
    }

}
